<?php

namespace app\checkbill\logic;
use app\common\library\RedissLock;
use app\common\service\CommonSystemImportService;
use think\facade\Db;
class CheckMonthOldLogic extends BasicLogic
{




    /**
     * @desc   获取列表数据
     * @author luckyXiong
     * @date   2024-06-07
     */
    public function get_list($params=[]){
        $shop_id   = $params['shop_id'] ?? '';
        $date      = $params['date'] ?? '';
        $limit     = $params['limit'] ?? 10;

        $where = [];
        $order = ['create_time desc'];
        if ($shop_id) {
            $where[] = ['shop_id', '=', $shop_id];
        }

        if ($date) {
            $where[] = ['date', '=', $date];
        }

        $res = Db::name('month_check')
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $list = $res->items()?:[];
        $list = objectToArray($list);


        //批量获取店铺信息及sku数量信息;
        $shop_ids      = array_column($list,'shop_id');
        $shopInfos     = Db::name('shops')->whereIn('id',$shop_ids)->field('name,id')->select()->toArray();
        $shopInfos_map = array_column($shopInfos,null,'id');
        foreach($list as $k=>$v){
            $shop_name               = $shopInfos_map[$v['shop_id']]??[];
            $list[$k]['shop_name']   = $shop_name['name']??'';
            $list[$k]['import_status'] = '导入成功';
        }

        return [
            'list'          => $list,
            'total'         => $res->total()

        ];
    }



    /**
     * @desc   月度账单导入老模版
     * @date    2024-06-05
     * @author  luckyXiong
     */
    public function uploadInfo($params){
        try{
            $result = CommonSystemImportService::staticModel()->operate_import_data([
                    $this,//当前对应的service
                    'save',//类型: template 下载模板; save :保存 导入
                    CommonSystemImportService::TYPE_IMPORT_MONTH_OLD,
                    1,
                    $params
                ]
            );
        }catch (\Exception $e) {
            return $this->jsonMessage(201, false, $e->getMessage(), null);
        }
    }



    /**
     * @desc 月度账单导入老模版
     * @date 2024-06-05
     * @author  luckyXiong
     */
    public function import_info($params=[]){
        $key = 'import_info_month_old::'.time();
        $redis_lock                          = new RedissLock();
        $lock                                = $redis_lock::lock($key, $redis_lock::UNBLOCK, 500);
        $order_id                            = getIsset($params,'order_id');
        $quantity                            = getIsset($params,'quantity');
        $sku_name                            = getIsset($params,'sku_name');
        $total_settlement_amount             = getIsset($params,'total_settlement_amount');
        $total_revenue                       = getIsset($params,'total_revenue');
        $customer_payment                    = getIsset($params,'customer_payment');
        $subtotal_after_seller_discounts     = getIsset($params,'subtotal_after_seller_discounts');
        $customer_shipping_fee               = getIsset($params,'customer_shipping_fee');
        $platform_discount                   = getIsset($params,'platform_discount');
        $sales_tax_payment                   = getIsset($params,'sales_tax_payment');
        $retail_delivery_fee_payment         = getIsset($params,'retail_delivery_fee_payment');
        $shipping_fee_subsidy                = getIsset($params,'shipping_fee_subsidy');
        $customer_order_refund               = getIsset($params,'customer_order_refund');
        $refund_to_customer                  = getIsset($params,'refund_to_customer');


        $refund_subsidy_by_platform          = getIsset($params,'refund_subsidy_by_platform');
        $platform_discount_refund            = getIsset($params,'platform_discount_refund');
        $total_fees                          = getIsset($params,'total_fees');
        $transaction_fee                     = getIsset($params,'transaction_fee');
        $referral_fee                        = getIsset($params,'referral_fee');
        $refund_administration_fee           = getIsset($params,'refund_administration_fee');
        $shipping_fee                        = getIsset($params,'shipping_fee');
        $actual_shipping_fee                 = getIsset($params,'actual_shipping_fee');
        $platform_shipping_fee_discount      = getIsset($params,'platform_shipping_fee_discount');
        $actual_return_shipping_fee          = getIsset($params,'actual_return_shipping_fee');
        $affiliate_commission                = getIsset($params,'affiliate_commission');
        $affiliate_partner_commission        = getIsset($params,'affiliate_partner_commission');

        $sales_tax                           = getIsset($params,'sales_tax');
        $sales_tax_payment_neg               = getIsset($params,'sales_tax_payment_neg');
        $sales_tax_refund                    = getIsset($params,'sales_tax_refund');
        $retail_delivery_fee                 = getIsset($params,'retail_delivery_fee');
        $retail_delivery_fee_payment_neg     = getIsset($params,'retail_delivery_fee_payment_neg');
        $retail_delivery_fee_refund          = getIsset($params,'retail_delivery_fee_refund');
        $ajustment_amount                    = getIsset($params,'ajustment_amount');
        $related_order_id                    = getIsset($params,'related_order_id');
        $type                                = getIsset($params,'type');
        $statement_date                      = getIsset($params,'statement_date');
        $statement_id                        = getIsset($params,'statement_id');


        if(preg_match('/^7\d+/', $order_id)){
            $order_id = $related_order_id;
        }

        $shop_info            = Db::name('order')->where('order_id',$order_id)->field('createtime,shop_id')->find();
        $shop_id              = $shop_info['shop_id']??0;
        $bach_id              = self::get_last_month().$shop_id;
        $create_time          = date("Y-m-d H:i:s", $shop_info['createtime']);

        //数据入库逻辑
        $info = [
            'batch_id'                         => $bach_id,
            'shop_id'                          => $shop_info['shop_id']??0,
            'order_id'                         => $order_id,
            'sku_name'                         => $sku_name=='/' ? '' : $sku_name,
            'quantity'                         => $quantity=='/' ? 0 : $quantity,
            'sku_num'                          => $sku_name=='/' ? 0 : get_sku_nums($sku_name),
            'total_settlement_amount'          => $total_settlement_amount,
            'total_revenue'                    => $total_revenue,
            'customer_payment'                 => $customer_payment,
            'subtotal_after_seller_discounts'  => $subtotal_after_seller_discounts,
            'customer_shipping_fee'            => $customer_shipping_fee,
            'platform_discount'                => $platform_discount,
            'sales_tax_payment'                => $sales_tax_payment,
            'retail_delivery_fee_payment'      => $retail_delivery_fee_payment,
            'shipping_fee_subsidy'             => $shipping_fee_subsidy,
            'customer_order_refund'            => $customer_order_refund,
            'refund_to_customer'               => $refund_to_customer,
            'refund_subsidy_by_platform'       => $refund_subsidy_by_platform,
            'platform_discount_refund'         => $platform_discount_refund,
            'total_fees'                       => $total_fees,
            'transaction_fee'                  => $transaction_fee,
            'referral_fee'                     => $referral_fee,
            'refund_administration_fee'        => $refund_administration_fee,
            'shipping_fee'                     => $shipping_fee,
            'actual_shipping_fee'              => $actual_shipping_fee,
            'platform_shipping_fee_discount'   => $platform_shipping_fee_discount,
            'actual_return_shipping_fee'       => $actual_return_shipping_fee,
            'affiliate_commission'             => $affiliate_commission,
            'affiliate_partner_commission'     => $affiliate_partner_commission,
            'sales_tax'                        => $sales_tax,
            'sales_tax_payment_neg'            => $sales_tax_payment_neg,
            'sales_tax_refund'                 => $sales_tax_refund,
            'retail_delivery_fee'              => $retail_delivery_fee,
            'retail_delivery_fee_payment_neg'  => $retail_delivery_fee_payment_neg,
            'retail_delivery_fee_refund'       => $retail_delivery_fee_refund,
            'ajustment_amount'                 => $ajustment_amount,
            'type'                             => $type,
            'statement_date'                   => $statement_date,
            'statement_id'                     => $statement_id,
            'date'                             => date("Y-m",strtotime($statement_date)),
            'create_time'                      => $create_time,
        ];
        $res = Db::name('month_check_old_log')->insertGetId($info);
        $lock->unlock();
        $info['status'] = $res == 1 ? 1:2;
        return  $info;
    }



    /**
     * @desc   月度账单导入老模版
     * @date    2024-06-06
     * @author  luckyXiong
     */
    public function get_excel_row_data($row){
        return [
            'order_id'                         => empty($row['E'])? '': trim($row['E']),
            'sku_name'                         => empty($row['I'])? '': trim($row['I']),
            'quantity'                         => empty($row['G'])? '': trim($row['G']),
            'total_settlement_amount'          => empty($row['J'])? '': trim($row['J']),
            'total_revenue'                    => empty($row['K'])? '': trim($row['K']),
            'customer_payment'                 => empty($row['L'])? '': trim($row['L']),
            'subtotal_after_seller_discounts'  => empty($row['M'])? '': trim($row['M']),
            'customer_shipping_fee'            => empty($row['N'])? '': trim($row['N']),
            'platform_discount'                => empty($row['O'])? '': trim($row['O']),
            'sales_tax_payment'                => empty($row['Q'])? '': trim($row['Q']),
            'retail_delivery_fee_payment'      => empty($row['R'])? '': trim($row['R']),
            'shipping_fee_subsidy'             => empty($row['P'])? '': trim($row['P']),
            'customer_order_refund'            => empty($row['S'])? '': trim($row['S']),
            'refund_to_customer'               => empty($row['T'])? '': trim($row['T']),
            'refund_subsidy_by_platform'       => empty($row['U'])? '': trim($row['U']),
            'platform_discount_refund'         => empty($row['V'])? '': trim($row['V']),
            'total_fees'                       => empty($row['W'])? '': trim($row['W']),
            'transaction_fee'                  => empty($row['X'])? '': trim($row['X']),
            'referral_fee'                     => empty($row['Y'])? '': trim($row['Y']),
            'refund_administration_fee'        => empty($row['Z'])? '': trim($row['Z']),
            'shipping_fee'                     => empty($row['AA'])? '': trim($row['AA']),
            'actual_shipping_fee'              => empty($row['AB'])? '': trim($row['AB']),
            'platform_shipping_fee_discount'   => empty($row['AC'])? '': trim($row['AC']),
            'actual_return_shipping_fee'       => empty($row['AD'])? '': trim($row['AD']),
            'affiliate_commission'             => empty($row['AE'])? '': trim($row['AE']),
            'affiliate_partner_commission'     => empty($row['AF'])? '': trim($row['AF']),
            'sales_tax'                        => empty($row['AG'])? '': trim($row['AG']),
            'sales_tax_payment_neg'            => empty($row['AH'])? '': trim($row['AH']),
            'sales_tax_refund'                 => empty($row['AI'])? '': trim($row['AI']),
            'retail_delivery_fee'              => empty($row['AJ'])? '': trim($row['AJ']),
            'retail_delivery_fee_payment_neg'  => empty($row['AK'])? '': trim($row['AK']),
            'retail_delivery_fee_refund'       => empty($row['AL'])? '': trim($row['AL']),
            'ajustment_amount'                 => empty($row['AM'])? '': trim($row['AM']),
            'related_order_id'                 => empty($row['AN'])? '': trim($row['AN']),
            'type'                             => empty($row['D'])? '' : trim($row['D']),
            'statement_date'                   => empty($row['A'])? '' : trim($row['A']),
            'statement_id'                     => empty($row['B'])? '' : trim($row['B']),
        ];
    }


    //获取上月
    public function get_last_month(){
        $year    = date('Y');
        $month   = date('m')-1;
        if($year=='2025' && $month==1){
             return '202412';
        }else{
            if($month>9){
                return $year.$month;
            }else{
                return $year.'0'.$month;
            }

        }
    }



}